package com.imu.purse.modules.base.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
/*import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;*/
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.imu.constant.Constant;
import com.imu.core.base.BaseService;
import com.imu.purse.modules.base.dao.DictInfoMapper;
import com.imu.purse.modules.base.entity.DictInfo;
import com.imu.purse.modules.base.service.DictInfoService;
import com.xiaoleilu.hutool.util.ArrayUtil;
import com.xiaoleilu.hutool.util.NumberUtil;
import com.xiaoleilu.hutool.util.StrUtil;

/**
 * 数据字典
 * @author sunhongwei
 * @email sunhongwei@jshijian.com
 * @date 2017-11-03 20:34:20
 * Copyright (c) 2017 武汉极时间科技有限公司 版权所有 
 * Jshijian aloha CO.,LTD. All Rights Reserved. 
 */
@Service
public class DictInfoServiceImpl extends BaseService<DictInfo> implements DictInfoService 
{
	@Override
    @Transactional
 	//@CacheEvict(value = CACHE, allEntries = true)
    public int save(final DictInfo sysDictInfo)
	{
		redisTemplate.delete(Constant.SYS_DICT);
		sysDictInfo.setCs(System.currentTimeMillis());
    	sysDictInfo.setMs(System.currentTimeMillis());
    	return super.insert(sysDictInfo);
	}
	
	@Override
	@Transactional
 	//@CacheEvict(value = CACHE, allEntries = true)
    public int update(final DictInfo sysDictInfo) 
    {
		redisTemplate.delete(Constant.SYS_DICT);
    	sysDictInfo.setMs(System.currentTimeMillis());
    	return mapper.updateByPrimaryKeySelective(sysDictInfo);
    }
    
    @Override
	@Transactional
    public int saveUpdate(final DictInfo sysDictInfo)
    {
    	int len = 0;
    	
    	if(1 > (len = this.update(sysDictInfo)))
    	{
        	len = this.insert(sysDictInfo);
    	}
    	
    	return len;
    }
    
 	@Override
	@Transactional
 	//@CacheEvict(value = CACHE, allEntries = true)
    public boolean delete(final String ...ids)
    {
    	if(ArrayUtil.isEmpty(ids)) return false;
    	
    	for (String id : ids) 
    	{
    		if(StrUtil.isBlank(id)) continue;
    		
    		if(NumberUtil.isNumber(id))
    		{
        		mapper.deleteByPrimaryKey(Long.valueOf(id));
    		}
		}

		redisTemplate.delete(Constant.SYS_DICT);
    	return true;
    }
    
    @Override
    //@Cacheable(value = CACHE, key = CACHE_INFO_KEY + "+#id")
    public DictInfo findById(final Long id)
    {
    	if(null == id) return null;
    	return mapper.selectByPrimaryKey(id);
    }
    
    @Override
    //@Cacheable(value = CACHE, key = CACHE_INFO_KEY + "+#code")
    public List<DictInfo> findByCode(final String code)
    {
    	if(StrUtil.isBlank(code)) return null;
    	
    	return mapper.findByCode(code, null);
    }
    
    @Override
    public List<DictInfo> findCahceByCode(final String code)
    {
    	if(StrUtil.isBlank(code)) return null;
    	
		String key = Constant.SYS_DICT;
		HashOperations<String, String, String> operations = redisTemplate.opsForHash();
    	String infos = operations.get(key, code);

    	if(StrUtil.isBlank(infos))
    	{
    		List<DictInfo> dtos = mapper.findByCode(code, null);
    		if(com.xiaoleilu.hutool.collection.CollectionUtil.isNotEmpty(dtos))
    		{
        		operations.put(key, code, JSON.toJSONString(dtos));
    		}
    		return dtos;
    	}
		
    	return JSON.parseArray(infos, DictInfo.class);
    }
    
    @Override
    public List<DictInfo> queryList(final DictInfo sysDictInfo)
    {
    	if (sysDictInfo.getPage() != null && sysDictInfo.getLimit() != null)
    	{
            PageHelper.startPage(sysDictInfo.getPage(), sysDictInfo.getLimit());
        }
    	
    	return mapper.findByPage(sysDictInfo);
    }
    
	@Autowired
	private DictInfoMapper mapper;
	
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
	
	/**
     * 缓存的key
     */
    public static final String CACHE = "dictInfoCache";
    
    /**
     * 缓存的key
     */
    public static final String CACHE_INFO_KEY = "\"DICT_INFO\"";
}
